<script>
import vendor from "@/views/former/vendor/vendor.vue";

export default {
  name: "Fabu",
  data() {
    return {
      zhi:'',
      zhi1:'',
      //数组
      options: [

      ],
      //商户表单
      businesses:{
        isBusiness:'',
        commodityId:'',
      },
      //供应商表单
      vendor:{
        commodityId:'',
      },
      //图片
      imageUrl: '',
      imageUrl1: '',
      optionss:{
    emitPath: true,
        value: 'id',
        label: 'categoryName',
        children: 'jis'
      }
    }
  },
  methods: {
      handleChange(value) {
        //拼接
        this.businesses.categoryId=value[1]
        console.log("this.businesses",this.businesses.categoryId)
      },
      //查询二级
      designFileChaXun() {
        this.$axios.get('productCategory/selectAll',).then(res=>{
          this.options=res.data
          console.log("this.options",this.options)
        })
      },
    //添加商品
    handleReset() {
    //获取当时间
      let date = new Date();
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      let day = date.getDate();this.businesses.createTime = date.getFullYear() + '' + (date.getMonth() + 1) + '' + date.getDate() + ' ' ;
      //打印日期
      console.log(555555,this.businesses.createTime)
      //转换时间格式
      this.businesses.commodityId = year + '' + month + '' + day;
     //打印表单数据
      console.log(99999,this.businesses)
      //打印图片路径
      console.log(777777,this.imageUrl,this.imageUrl1)
      this.$axios.post('Commodity/insert',this.businesses).then(res=>{
        this.vendor.commodityId=res.data
        this.vendor.imageType=1
        this.vendor.image=this.businesses.userAvatar
        this.$axios.post('Commodity/insertImage',this.vendor).then(res=>{
         //提示
          this.$message({
            message: '添加成功',
            type: 'success'
          })
        })
        this.vendor.imageType=2
        this.vendor.image=this.businesses.userAvatar1
        this.$axios.post('Commodity/insertImage',this.vendor).then(res=>{
        })
      })
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.businesses.userAvatar =res;
      this.vendor.userAvatar =res;
      console.log("this.businesses",this.businesses)
    },
    handleAvatarSuccess1(res, file) {
      this.imageUrl1 = URL.createObjectURL(file.raw);
      this.businesses.userAvatar1 =res;
      this.vendor.userAvatar =res;
      console.log("this.businesses",this.businesses)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  },
  created() {
    this.designFileChaXun()
  },
  watch:{
    // 监听下拉框
    value()
    {
      this.designFileChaXun()
    }
  }
}
</script>

<template>
  <div id="Fabu">
    <!--添加商品-->
    <el-card>
      <el-descriptions  class="margin-top" title="填写商品信息" :column="2" border>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-picture-outline"></i>
            商品主图
          </template>
          <el-upload
              v-model="vendor.picture"
              name="picture"
              class="avatar-uploader"
              action="http://localhost:9999/zhuche/image"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-picture-outline"></i>
            商品副图
          </template>
          <el-upload
              v-model="vendor.picture1"
              name="picture"
              class="avatar-uploader"
              action="http://localhost:9999/zhuche/image"
              :show-file-list="false"
              :on-success="handleAvatarSuccess1"
              :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl1" :src="imageUrl1" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-user"></i>
            商品名称
          </template>
          <!--商品名称--->
          <el-input
              v-model="businesses.commodityName"
              type="text"
              :disabled="rr"
              placeholder="请输入名称">
          </el-input>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-s-custom"></i>
            商品价格
          </template>
         <!--商品价格--->
          <el-input
              v-model="businesses.commodityPrice"
              type="text"
              :disabled="rr"
              placeholder="请输入价格">
          </el-input>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-s-custom"></i>
            分类
          </template>
          <el-cascader
                        :options="options"
                       :props="optionss"
                       @change="handleChange"
          ></el-cascader>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-magic-stick"></i>
            商品描述
          </template>
          <el-input
              v-model="businesses.commodityDesc"
              type="textarea"
              :disabled="rr"
              :rows="2"
              placeholder="请输入内容">
          </el-input>
        </el-descriptions-item>
        <el-descriptions-item>
          <el-button style="width: 100px;background-color:#B3C0D1;border: gray"  @click="handleReset">添加</el-button>
        </el-descriptions-item>
      </el-descriptions>
    </el-card>
  </div>
</template>

<style>
#Fabu {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>